package com.wgts.wgts.mvp.view.activity.classification;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerClassificationActivityComponent;
import com.wgts.wgts.dagger.module.ClassificationActivityModule;
import com.wgts.wgts.mvp.presenter.IClassificationActivityPresenter;
import com.wgts.wgts.mvp.view.IClassificationActivityView;
import com.wgts.wgts.mvp.view.activity.search.SearchActivity;
import com.wgts.wgts.netmodle.resp.AllClassTypeEntity;
import com.wgts.wgts.netmodle.resp.ClassificationDetailEntity;
import com.wgts.wgts.netmodle.resp.ClassificationEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 分类页面
 */
public class ClassificationActivity extends BaseActivity implements IClassificationActivityView{


    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.linear_search)
    LinearLayout linearSearch;
    @BindView(R.id.recycle_classification)
    RecyclerView recycleClassification;
    @BindView(R.id.recycle_classification_detail)
    RecyclerView recycleClassificationDetail;
    private CustomLineaLayoutManager linearLayoutManager;

    private ClassificationAdapter adapter;

    private ClassificationDetailAdapter classificationDetailAdapter;


    private List<AllClassTypeEntity> firstEntity;

    private List<AllClassTypeEntity.Entity> secondEntity;


    @Inject
    IClassificationActivityPresenter presenter;

    @Override
    public void init() {

        DaggerClassificationActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .classificationActivityModule(new ClassificationActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onCreate(Bundle saveInstanceBundle) {
        super.onCreate(saveInstanceBundle);
        setContentView(R.layout.classification_activity);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        linearLayoutManager = new CustomLineaLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleClassification.setLayoutManager(linearLayoutManager);


        linearLayoutManager = new CustomLineaLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleClassificationDetail.setLayoutManager(linearLayoutManager);

        presenter.initClassType();

    }


    @OnClick({R.id.linear_back, R.id.linear_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.linear_search:
                Intent intent = new Intent(ClassificationActivity.this, SearchActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void initResult(List<AllClassTypeEntity> entity) {

        firstEntity = entity;
        secondEntity = new ArrayList<>();


        if (!entity.isEmpty()){
            entity.get(0).setSelect(true);
            secondEntity.clear();
            secondEntity.addAll(entity.get(0).subTypes);
            //secondEntity = entity.get(0).subTypes;
            classificationDetailAdapter = new ClassificationDetailAdapter(secondEntity, this);

        }



        adapter = new ClassificationAdapter(firstEntity, this);
        recycleClassification.setAdapter(adapter);
        adapter.setOnItemClickView(new ClassificationAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                for (int i = 0; i < firstEntity.size(); i++) {
                    firstEntity.get(i).setSelect(false);
                }
                firstEntity.get(postion).setSelect(true);
                adapter.notifyDataSetChanged();
                secondEntity.clear();
                secondEntity.addAll(firstEntity.get(postion).subTypes);
                //secondEntity = firstEntity.get(postion).subTypes;
                classificationDetailAdapter.notifyDataSetChanged();
            }
        });


        recycleClassificationDetail.setAdapter(classificationDetailAdapter);
    }

    @Override
    public void onError(int status, String msg) {

    }
}
